﻿using System;
using System.Collections.Generic;
using System.Data; 
using System.Text;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Configuration;
using System.Data.Common;

/// <summary>
///CodeHelper 的摘要说明
/// </summary>
public class CodeHelper
{
 
    // Fields
    public StringBuilder dao = new StringBuilder();
    private Database db = DatabaseFactory.CreateDatabase(ConfigurationManager.AppSettings["ConfigName"]);
    public StringBuilder detailaspx = new StringBuilder();
    public StringBuilder detailaspxcs = new StringBuilder();
    public StringBuilder iservice = new StringBuilder();
    public StringBuilder model = new StringBuilder();
    private ServiceHelper service = new ServiceHelper();
    public StringBuilder strservice = new StringBuilder();
    public StringBuilder tablexml = new StringBuilder();
    public StringBuilder viewaspx = new StringBuilder();
    public StringBuilder viewaspxcs = new StringBuilder();

 

    public void BulidAppCode(string classname, string table, string nameSpace)
    {
        CreatFile.CreateFileHelper(classname + "Service.cs", ServiceHelper.BuildService(classname, table, nameSpace));
        CreatFile.CreateFileHelper("I" + classname + "Service.cs", ServiceHelper.BuildIservice(classname, table, nameSpace));
        CreatFile.CreateFileHelper(classname + "Dao.cs", ServiceHelper.BuildDao(classname, table, this.GetDbColumn(table), nameSpace));
        CreatFile.CreateFileHelper(classname + "Model.cs", ServiceHelper.BuildModel(classname, table, this.GetDbColumn(table), nameSpace));
        //CreatFile.CreateFileHelper(classname + "List.aspx", ServiceHelper.BuildListASPX(classname, table, this.GetDbColumn(table)));
        //CreatFile.CreateFileHelper(classname + "List.aspx.cs", ServiceHelper.BuildListASPXCS(classname, table));
        //CreatFile.CreateFileHelper(classname + "List.aspx.designer.cs", ServiceHelper.BuildListDesign(classname, table));
        CreatFile.CreateFileHelper(classname + "Detail.aspx", ServiceHelper.BuildDETAILASPX(classname, table, this.GetDbColumn(table)));
        CreatFile.CreateFileHelper(classname + "Detail.aspx.cs", ServiceHelper.BuildDETAILASPXCS(classname, table, this.GetDbColumn(table)));
        CreatFile.CreateFileHelper(classname + "Detail.aspx.designer.cs", ServiceHelper.BuildDETAILASPXDESIGN(classname, table, this.GetDbColumn(table)));
        this.tablexml.Append(ServiceHelper.BuildColumnXML(classname, table, this.GetDbColumn(table), nameSpace));
    }

    public void BulidSiteCode(string classname, string table, string nameSpace)
    {
        this.strservice.Append(ServiceHelper.BuildService(classname, table, nameSpace));
        this.iservice.Append(ServiceHelper.BuildIservice(classname, table, nameSpace));
        this.dao.Append(ServiceHelper.BuildDao(classname, table, this.GetDbColumn(table), nameSpace));
        this.model.Append(ServiceHelper.BuildModel(classname, table, this.GetDbColumn(table), nameSpace));
        //this.tablexml.Append(ServiceHelper.BuildColumnXML(classname, table, this.GetDbColumn(table), nameSpace));
       // this.detailaspx.Append(ServiceHelper.BuildSiteDETAILASPX(classname, table, this.GetDbColumn(table)));
       // this.detailaspxcs.Append(ServiceHelper.BuildDETAILSiteASPXCS(classname, table, this.GetDbColumn(table)));
       // this.viewaspx.Append(ServiceHelper.BuildSiteViewASPX(classname, table, this.GetDbColumn(table)));
        //this.viewaspxcs.Append(ServiceHelper.BuildDSiteViewASPXCS(classname, table, this.GetDbColumn(table)));
    }

    private DataTable GetDbColumn(string table)
    {
        string format = @"SELECT       a.name as cname ,      b.name  as typename,     a.isnullable, c.value as descption  FROM dbo.syscolumns a LEFT OUTER JOIN    
                        dbo.systypes b ON a.xtype = b.xusertype INNER JOIN
                        dbo.sysobjects d ON a.id = d.id AND  (d.xtype = 'U' or d.xtype='V')  AND 
                        d.status >= 0       
                        left join sys.extended_properties c on a.id=c.major_id  
                        and a.colid=c.minor_id    where d.name='{0}' ORDER BY d.name, a.colorder";
        format = string.Format(format, table);
        DbCommand command = db.GetSqlStringCommand(format);
        return db.ExecuteDataSet(command).Tables[0];
    }

    public DataTable GetDBTables()
    {
        string commandText = "select name,xtype from sysobjects where OBJECTPROPERTY(id, N'IsUserTable') = 1 or  type='V'  order by xtype,name";
        DbCommand command = db.GetSqlStringCommand(commandText);
        return db.ExecuteDataSet(command).Tables[0];
    }
}

